<template>
  <a-modal
    :title="title"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="model" :form="form" :rules="rules" ref="form">
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="组织" prop="company">
              <j-dict-select-tag dictCode="crm_company,name,id" v-model="model.company" placeholder="请选择组织">
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="车间" prop="workshop">
              <j-dict-select-tag
                placeholder="请选择车间"
                v-model="model.workshop"
                dictCode="workshop"
                @change="changeWorkShop"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="楼层" prop="floor">
              <j-dict-select-tag dictCode="floor" v-model="model.floor" placeholder="请选择楼层"> </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="络筒品种" prop="productType">
              <j-dict-select-tag
                placeholder="请选择络筒品种"
                v-model="model.productType"
                @change="changePz"
                dictCode="crm_product_type_spooling,product_type_full_name,id"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="络筒机台" prop="spoolingDeviceIds">
              <a-space>
                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.spoolingDeviceText"></a-input>
                <a-button type="primary" icon="plus" size="small" @click="showModal('equipmentScopeLt')"></a-button>
              </a-space>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <!--            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="欠开(24)">-->
            <!--              <a-input placeholder="请选择" readOnly v-model="model.unOpen"></a-input>-->
            <!--            </a-form-model-item>-->
            <!--            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="锭数" prop="ingotslds">-->
            <!--              <a-space>-->
            <!--                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.ingotslds"></a-input>-->
            <!--                <a-button type="primary" icon="plus" size="small" @click="showDingModal(model.ingotslds)"></a-button>-->
            <!--              </a-space>-->
            <!--            </a-form-model-item>-->
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="甲A开台" prop="adeviceIds">
              <a-space>
                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.adeviceText"></a-input>
                <a-button type="primary" icon="plus" size="small" @click="ktOpen('jakt')"></a-button>
              </a-space>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="甲A品种">
              <j-dict-select-tag
                placeholder="请选择"
                v-model="model.aproductType"
                dictCode="crm_product_type_spooling,product_type_full_name,id"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="甲A开始时间" prop="aStartTime">
              <a-space>
                <a-date-picker
                  show-time
                  v-model:value="formAStartTime"
                  placeholder="选择日期"
                  style="width:215px"
                  :allowClear="false"
                  @change="changeAStartTime"
                />
              </a-space>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="甲A结束时间" prop="aEndTime">
              <a-date-picker
                show-time
                v-model:value="formAEndTime"
                placeholder="选择日期"
                style="width:215px"
                :allowClear="false"
                @change="changeAEndTime"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="乙B开台" prop="bdeviceIds">
              <a-space>
                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.bdeviceText"></a-input>
                <a-button type="primary" icon="plus" size="small" @click="ktOpen('ybkt')"></a-button>
              </a-space>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="乙B品种">
              <j-dict-select-tag
                placeholder="请选择"
                v-model="model.bproductType"
                dictCode="crm_product_type_spooling,product_type_full_name,id"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="乙B开始时间" prop="aStartTime">
              <a-space>
                <a-date-picker
                  show-time
                  v-model:value="formBStartTime"
                  placeholder="选择日期"
                  style="width:215px"
                  :allowClear="false"
                  @change="changeBStartTime"
                />
              </a-space>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="乙B结束时间" prop="aEndTime">
              <a-date-picker
                show-time
                v-model:value="formBEndTime"
                placeholder="选择日期"
                style="width:215px"
                :allowClear="false"
                @change="changeBEndTime"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="入库数量" prop="inQuantity">
              <a-input-number v-model:value="model.inQuantity" style="width:215px"></a-input-number>
            </a-form-model-item>
          </a-col>
        </a-row>
        <!--        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">-->
        <!--          <a-col :span="11">-->
        <!--            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="丙C开台" prop="cdeviceIds">-->
        <!--              <a-space>-->
        <!--                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.cdeviceText"></a-input>-->
        <!--                <a-button type="primary" icon="plus" size="small" @click="ktOpen('bckt')"></a-button>-->
        <!--              </a-space>-->
        <!--            </a-form-model-item>-->
        <!--          </a-col>-->
        <!--          <a-col :span="11">-->
        <!--            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="丙C品种">-->
        <!--              <j-dict-select-tag placeholder="请选择" v-model="model.cproductType"-->
        <!--                                 dictCode="crm_product_type_spooling,product_type_full_name,id"/>-->
        <!--            </a-form-model-item>-->
        <!--          </a-col>-->
        <!--        </a-row>-->
        <!--        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">-->
        <!--          <a-col :span="11">-->
        <!--            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="丙C开始时间" prop="cStartTime">-->
        <!--              <a-space>-->
        <!--                <a-date-picker show-time v-model:value="formCStartTime" placeholder="选择日期" style="width:215px" :allowClear="false"-->
        <!--                               @change="changeCStartTime"/>-->
        <!--              </a-space>-->
        <!--            </a-form-model-item>-->
        <!--          </a-col>-->
        <!--          <a-col :span="11">-->
        <!--            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="丙C结束时间" prop="cEndTime">-->
        <!--              <a-date-picker show-time v-model:value="formCEndTime" placeholder="选择日期" style="width:215px" :allowClear="false"-->
        <!--                             @change="changeCEndTime"/>-->
        <!--            </a-form-model-item>-->
        <!--          </a-col>-->
        <!--        </a-row>-->
      </a-form-model>
    </a-spin>
    <luotong-jitai-modal ref="luotongJitaiModal" @getDevSnArr="getDevSnArr"></luotong-jitai-modal>
    <!--    <dingshu-modal ref="dingshuModal"  @btnSure="btnSure"></dingshu-modal>-->
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import LuotongJitaiModal from './LuotongJitaiModal'
import DingshuModal from './DingshuModal'
import JEditor from '@/components/jeecg/JEditor'
import JSwitch from '@/components/jeecg/JSwitch'
import { message } from 'ant-design-vue'
import moment from 'moment'

export default {
  name: 'QinghuaModal',
  components: {
    JEditor,
    JSwitch,
    LuotongJitaiModal,
    DingshuModal
  },
  props: {
    planTime: String
  },
  data() {
    return {
      equipmentScopeText: '',
      formAStartTime: moment(new Date()).format('YYYY-MM-DD') + ' 00:00:00',
      formAEndTime: moment(new Date()).format('YYYY-MM-DD') + ' 8:00:00',
      formBStartTime: moment(new Date()).format('YYYY-MM-DD') + ' 8:00:00',
      formBEndTime: moment(new Date()).format('YYYY-MM-DD') + ' 16:00:00',
      formCStartTime: moment(new Date()).format('YYYY-MM-DD') + ' 16:00:00',
      formCEndTime: moment(new Date()).format('YYYY-MM-DD') + ' 24:00:00',
      adds: {
        org: '',
        cj: '',
        devSnArea: [],
        equipmentScope: [],
        equipmentScopeLt: [],
        lc: '',
        pz: '',
        pzCs: '',
        qk: 0,
        time: '',
        jakt: [],
        ybkt: [],
        bckt: [],
        pzja: '',
        pzyb: '',
        pzbc: ''
      },
      title: '操作',
      visible: false,
      disable: true,
      model: {
        company: null,
        workshop: null,
        floor: null,
        productTypeRove: null,
        roveDeviceIds: '',
        roveDeviceText: '',
        spunYarnDeviceIds: '',
        spunYarnDeviceText: '',
        productType: null,
        spoolingDeviceIds: '',
        spoolingDeviceText: '',
        adeviceIds: '',
        adeviceText: '',
        aproductType: null,
        bdeviceIds: '',
        bdeviceText: '',
        bproductType: null,
        cdeviceIds: '',
        cdeviceText: '',
        cproductType: null,
        unOpen: 0,
        ingotslds: '',
        aStartTime: moment(new Date()).format('YYYY-MM-DD') + ' 00:00:00',
        aEndTime: moment(new Date()).format('YYYY-MM-DD') + ' 8:00:00',
        bStartTime: moment(new Date()).format('YYYY-MM-DD') + ' 8:00:00',
        bEndTime: moment(new Date()).format('YYYY-MM-DD') + ' 16:00:00',
        cStartTime: moment(new Date()).format('YYYY-MM-DD') + ' 16:00:00',
        cEndTime: moment(new Date()).format('YYYY-MM-DD') + ' 24:00:00',
        // planTime: moment(new Date()).format('YYYY-MM-DD'),
        planTime: this.planTime
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 9 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      rules: {
        company: [{ required: true, message: '请选择组织', trigger: 'blur' }],
        workshop: [{ required: true, message: '请选择车间', trigger: 'blur' }],
        floor: [{ required: true, message: '请选择楼层', trigger: 'blur' }],
        productType: [{ required: true, message: '请选择品种', trigger: 'blur' }],
        spoolingDeviceIds: [{ required: true, message: '请选择', trigger: 'blur' }],
        ingotslds: [{ required: true, message: '请选择', trigger: 'blur' }],
        adeviceIds: [{ required: true, message: '请选择', trigger: 'blur' }],
        bdeviceIds: [{ required: true, message: '请选择', trigger: 'blur' }],
        cdeviceIds: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      url: {
        add: '/crm_daily_plan_spooling/crmDailyPlanSpooling/add',
        edit: '/crm_daily_plan_spooling/crmDailyPlanSpooling/edit'
      }
    }
  },
  created() {},
  methods: {
    add() {
      this.disable = false
      this.adds = {
        org: '',
        cj: '',
        devSnArea: [],
        equipmentScope: [],
        equipmentScopeLt: [],
        lc: '',
        pz: '',
        pzCs: '',
        qk: 0,
        time: '',
        jakt: [],
        ybkt: [],
        bckt: [],
        pzja: '',
        pzyb: '',
        pzbc: ''
      }
      this.model.company = '1771136798305476610'
      this.model.floor = 2
      this.formAStartTime = this.planTime + ' 00:00:00'
      this.formAEndTime = this.planTime + ' 12:00:00'
      this.formBStartTime = this.planTime + ' 12:00:00'
      this.formBEndTime = this.planTime + ' 24:00:00'

      this.model.aStartTime = this.planTime + ' 00:00:00'
      this.model.aEndTime = this.planTime + ' 12:00:00'
      this.model.bStartTime = this.planTime + ' 12:00:00'
      this.model.bEndTime = this.planTime + ' 24:00:00'
      this.model.planTime = this.planTime
      this.edit({}, 1)
    },
    edit(record, isEdit) {
      if (isEdit != 1) {
        this.model = Object.assign({}, record)
      }

      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disable = true
      this.model.roveDeviceText = ''
      this.model.spoolingDeviceIds = ''
      this.model.spoolingDeviceText = ''
      this.model.adeviceIds = ''
      this.model.adeviceText = ''
      this.model.aproductType = null
      this.model.bdeviceIds = ''
      this.model.bdeviceText = ''
      this.model.bproductType = null
      this.model.cdeviceIds = ''
      this.model.cdeviceText = ''
      this.model.cproductType = null
      this.model.ingotslds = ''
      this.$refs.form.resetFields()
    },
    changeAStartTime(date, dateString) {
      this.model.aStartTime = dateString
    },
    changeAEndTime(date, dateString) {
      this.model.aEndTime = dateString
    },
    changeBStartTime(date, dateString) {
      this.model.bStartTime = dateString
    },
    changeBEndTime(date, dateString) {
      this.model.bEndTime = dateString
    },
    changeCStartTime(date, dateString) {
      this.model.cStartTime = dateString
    },
    changeCEndTime(date, dateString) {
      this.model.cEndTime = dateString
    },
    //切换车间
    changeWorkShop(val) {
      this.adds.workshop = val
    },
    handleOk() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model)
          console.log('formData', formData)
          //时间格式化
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.close()
            })
        } else {
          return false
        }
      })
    },

    handleCancel() {
      this.close()
    },
    ktOpen(param) {
      this.$refs.luotongJitaiModal.show(this.adds, param, true)
    },
    showModal(param) {
      if (this.model.workshop && this.model.workshop != 'null') {
        this.$refs.luotongJitaiModal.show(this.adds, param, true)
      } else {
        message.warning('请先选择车间！')
      }
    },
    showDingModal() {
      this.$refs.dingshuModal.show(this.model.ingotslds, '', true)
    },
    // 切换pz
    changePz(val) {
      this.add.pzja = val
      this.add.pzyb = val
      this.add.pzbc = val
      this.model.aproductType = val
      this.model.bproductType = val
      this.model.cproductType = val
    },
    btnSure(selectIds, selectString) {
      console.log(selectIds)
      console.log(selectString)
      this.model.ingotslds = selectString
    },
    getDevSnArr(params, arr, choseDevideIds) {
      this.adds[params] = arr
      arr.forEach(item => {
        item.devSnArea.sort((a, b) => parseInt(a.devSn) - parseInt(b.devSn))
      })
      if (params == 'equipmentScopeLt') {
        this.model.spoolingDeviceText = this.getSeiresDevsnCs(arr)
        this.model.spoolingDeviceIds = choseDevideIds.join(',')

        if (this.model.adeviceIds == '') {
          this.adds['jakt'] = arr
          this.model.adeviceText = this.getSeiresDevsnCs(arr)
          this.model.adeviceIds = choseDevideIds.join(',')
        }
        if (this.model.bdeviceIds == '') {
          this.adds['ybkt'] = arr
          this.model.bdeviceText = this.getSeiresDevsnCs(arr)
          this.model.bdeviceIds = choseDevideIds.join(',')
        }
        if (this.model.cdeviceIds == '') {
          this.adds['bckt'] = arr
          this.model.cdeviceText = this.getSeiresDevsnCs(arr)
          this.model.cdeviceIds = choseDevideIds.join(',')
        }
      } else if (params == 'jakt') {
        this.model.adeviceText = this.getSeiresDevsnCs(arr)
        // this.model.adeviceText = this.getSeiresDevsn(arr);
        this.model.adeviceIds = choseDevideIds.join(',')
      } else if (params == 'ybkt') {
        // this.model.bdeviceText = this.getSeiresDevsn(arr);
        this.model.bdeviceText = this.getSeiresDevsnCs(arr)
        this.model.bdeviceIds = choseDevideIds.join(',')
      } else if (params == 'bckt') {
        // this.model.bdeviceText = this.getSeiresDevsn(arr);
        this.model.cdeviceText = this.getSeiresDevsnCs(arr)
        this.model.cdeviceIds = choseDevideIds.join(',')
      }
      this.calQk()
    },
    calQk() {
      let devSn = this.adds.devSnArea.length
      let ja = this.adds.jakt.length
      let yb = this.adds.ybkt.length
      this.adds.qk = devSn - (ja + yb) * 0.5
      this.model.unOpen = devSn - (ja + yb) * 0.5
    },
    // 获取连续机台-粗纱
    getSeiresDevsnCs(arr) {
      let str = ''
      arr.map(item => {
        let itemStr = `${item.cj}：${this.getSeiresDevsn(item.devSnArea)}`
        str += itemStr + '；'
      })
      str = str.substring(0, str.length - 1)
      return str
    },
    // 获取连续车台
    getSeiresDevsn(arr) {
      if (arr) {
        var result = [],
          i = 0,
          str = ''
        if (arr.length > 0) {
          result[i] = [arr[0].devSn]
          arr.reduce(function(prev, cur) {
            parseInt(cur.devSn) - parseInt(prev.devSn) == 1 ? result[i].push(cur.devSn) : (result[++i] = [cur.devSn])
            return cur
          })
          result.map(item => {
            if (item.length > 1) {
              str += `#${item[0]}~#${item[item.length - 1]},`
            } else {
              str += `#${item[0]},`
            }
          })
          str = str.substring(0, str.length - 1)
        } else {
          str = ''
        }
        return str
      }
    }
  }
}
</script>

<style scoped></style>
